<template>
  <div class="login_container">
    <div class="login_box">
      <div style="padding:20px;background-color: white;margin-left: 100px;border-radius: 20px;box-shadow:0 0  10px rgba(0,0,0.1)">
        <el-form ref="formRef" :rules="data.rules" :model="data.form" style="width:400px"  autocomplete="off" >
          <div style="margin-bottom:30px;font-size:20px;color:#0742b1;font-weight:bolder;text-align: center">欢迎登录后台管理系统</div>
            <el-form-item style="margin-top: 20px;" prop="username" autocomplete="off">
              <el-input size="large"   v-model="data.form.username" placeholder="请输入账号" prefix-icon="User" >
              </el-input>
            </el-form-item>
            <el-form-item  prop="password">
              <el-input show-password="show-password" autocomplete="off" size="large" v-model="data.form.password" placeholder="请输入密码" prefix-icon="Lock">
              </el-input>
            </el-form-item>
          <el-form-item prop="role">
            <el-select v-model="data.form.role" placeholder="请选择角色" style="width: 100%">
              <el-option label="管理员" value="ADMIN"></el-option>
              <el-option label="员工" value="EMP" ></el-option>
            </el-select>
          </el-form-item>
          <div>
            <el-button size="large" style="width: 48%" type="primary" @click="login()">登 录</el-button>
            <el-button size="large" style="width: 48%" type="info">取 消</el-button>
          </div>
          <div style="text-align: right;margin-top: 20px;">
            还没有账号？请<a style="color: #0742b1;text-decoration: none;" href="/register">注册</a>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script setup>
import { reactive,ref} from "vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";

const res = ref()
const data = reactive({
  form:{
    username:'',
    password:'',
    role:'ADMIN'
  },
  rules:{
    username:[{required:true,message:'请输入账号',trigger:'blur'}],
    password:[{required:true,message:'请输入密码',trigger:'blur'}]
    }
})

const formRef = ref()
const login = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      request.post('/login', data.form)
          .then(res => {
            if (res.code === '200') {
              // res.data 现在是一个对象，包含了 token 和 user
              localStorage.setItem("jwt_token", res.data.token); // 单独存储 token
              localStorage.setItem("zwy-user", JSON.stringify(res.data.user)); // 存储完整的 user 对象
              ElMessage.success('登录成功');
              setTimeout(() => {
                location.href = '/manager/home'
              }, 1000)

            } else {
              ElMessage.error(res.msg); // 显示业务错误消息
            }
          })
          .catch(error => {
            if (error.response) {
              // 处理 HTTP 错误状态码（如 400）
              const { data } = error.response;
              if (data && data.msg) {
                ElMessage.error(data.msg); // 显示后端返回的错误消息
              } else {
                ElMessage.error('请求失败，请稍后重试');
              }
            } else {
              ElMessage.error('网络请求失败，请检查网络连接');
            }
          });
    }
  });
}

</script>
<!--scoped表示当页生效-->
<style scoped>
.login_container{
  height: 100vh;
  overflow:hidden;
  background-image: url("@/assets/login_bg.png");
  background-size:cover ;
  background-position: 0 -40px;
}
.login_box{
  width: 50%;
  height: 100%;
  display: flex;
  align-items: center;
  right: 0;
  position: absolute;
}
</style>
